package com.huey.utils.thread;


import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;


/**
 * Author: huey
 * Desc: Redisson配置   哨兵配置
 */
@Slf4j
//@Component
public class RedissonManager {

    private  static String masterName;

    private static String nodesAddress ;

    private static String password;

    private static Integer database;

    private static  Redisson redisson;

    @Autowired
    private RedissonManager (@Value("${spring.redis.sentinel.settlement.master:}") String masterName,
                             @Value("${spring.redis.sentinel.settlement.nodes:}")String nodesAddress,
                             @Value("${spring.redis.sentinel.settlement.password}")String password,
                             @Value("${spring.redis.sentinel.settlement.database}")Integer database){
        RedissonManager.masterName = masterName;
        RedissonManager.nodesAddress = nodesAddress;
        RedissonManager.password=password;
        RedissonManager.database=database;
    }



    private RedissonManager (){

    }

    public static Redisson  getInstance() {
        if(ObjectUtils.isEmpty(redisson)){
            init();
        }
        return redisson;
    }
    /**
     *  初始化Redisson对象
     */
    public static  void init() {
/*        //主从
        Config config = new Config();
        String[] address = nodesAddress.split(",");
        for (int i=0;i<address.length;i++) {
            address[i]= "redis://"+address[i];
        }
        config.useMasterSlaveServers()
                .setMasterAddress(address[0])
                .addSlaveAddress(address)
                .setDatabase(dateBase)
                .setPassword(password)
        ;
        redisson=(Redisson)Redisson.create(config);*/

        //哨兵
        Config config = new Config();
        String[] address = nodesAddress.split(",");
        for (int i=0;i<address.length;i++) {
            address[i]= "redis://"+address[i];
        }
        config.useSentinelServers()
                .setMasterName(masterName)
                .addSentinelAddress(address)
                .setDatabase(database)
                .setPassword(password)
        ;
        redisson=(Redisson)Redisson.create(config);

/*        //集群
        Config config = new Config();
        config.useClusterServers()
                .setScanInterval(2000)
                .addNodeAddress("127.0.0.1:7000", "127.0.0.1:7001")
                .addNodeAddress("127.0.0.1:7002");
        RedissonClient redisson = Redisson.create(config);*/

    }


}
